<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button id="replay">replay</button>
<script>
    var Ryu = {
        attack:function () {
            console.log('attack');
        },
        defense:function () {
            console.log('defense');
        },
        jump:function () {
            console.log('jump');
        },
        crouch:function () {
            console.log('crouch');
        }
    };

    var mackCommand = function (receiver, state) {
        return function () {
            receiver[state]();
        }
    };

    var commands = {
        '119':'jump',
        '115':'crouch',
        '97':'defense',
        '100':'attack'
    };

    var commandStack = [];

    document.onkeypress = function (ev) {
        var keyCode = ev.keyCode,command = mackCommand(Ryu,commands[keyCode]);
        if(command){
            command();
            commandStack.push(command);
        }
    };

    document.getElementById('replay').onclick = function () {
        var command;
        while (command=commandStack.shift()){
            command();
        }
    };
</script>
</body>
</html>